package com.smartcommunity.entity.village;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.smartcommunity.base.common.CacheMap;
import com.smartcommunity.bean.entity.Village;
import com.smartcommunity.bean.enumeration.HouseTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.smartcommunity.entity.people.RealPeopleHouse;
import com.smartcommunity.es.common.EsBasedataEnum;
import com.smartcommunity.utils.DateUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * es房屋实体类
 *
 * @author yangyi
 * @since 2023年6月20日
 */
@Getter
@Setter
public class EsHouseEntity {
	
	/**
	 * ID
	 */
    private Integer id;
    
    /**
     * 数据类型  1:实有人口  2:车辆 3:房屋  4:vid
     */
    private Integer dataType;
    
    /**
     * 小区编码
     */
    private String villageCode;
    
    /**
     * 人房关系
     */
    private List<RealPeopleHouse> detailAddress = new ArrayList<RealPeopleHouse>();
    
    /**
     * 房屋类型 0: 其他,1:自住房,2:出租房，3:闲置房，4.民宿，5.寄宿，6.借宿，7.单位宿舍，8.临时性宿舍，9.农民工公寓" 
     */
    private Integer houseType;
    /**
     * 入库时间
     */
    private Timestamp mrowTime;
	
	public String toString() {
        return JSON.toJSONString(this);
    }
	
	public static EsHouseEntity parseFrom(House house) {
		EsHouseEntity esHouseEntity = new EsHouseEntity();
		esHouseEntity.setDataType(EsBasedataEnum.HOUSE.getIntValue());
		esHouseEntity.setId(house.getId());
		esHouseEntity.setVillageCode(house.getVillageCode());
		esHouseEntity.setHouseType(house.getType());
		List<RealPeopleHouse> realPeopleHouses = new ArrayList<RealPeopleHouse>();
		RealPeopleHouse realPeopleHouse = new RealPeopleHouse();
		realPeopleHouse.setVillageCode(house.getVillageCode());
		realPeopleHouse.setBuildingNo(house.getBuildingNo());
		realPeopleHouse.setUnitNo(house.getUnitNo());
		realPeopleHouse.setHouseNo(house.getHouseNo());
		realPeopleHouse.setDetailAddress(realPeopleHouse.concatDetailAddress());
		realPeopleHouses.add(realPeopleHouse);
		esHouseEntity.setDetailAddress(realPeopleHouses);
		if(house.getMrowTime()==null) {
			esHouseEntity.setMrowTime(DateUtils.getNowTimestamp());
		}else {
			esHouseEntity.setMrowTime(new Timestamp(house.getMrowTime().getTime()));
		}
		return esHouseEntity;
	}
	
	public static House parseTo(EsHouseEntity esHouse, List<String> villageCode) {
		House house = new House();
		house.setId(esHouse.getId());
		house.setVillageCode(esHouse.getVillageCode());
		house.setType(esHouse.getHouseType());
		if(CollectionUtils.isNotEmpty(esHouse.getDetailAddress())) {
			for(RealPeopleHouse realPeopleHouse:esHouse.getDetailAddress()) {
				if(CollectionUtils.isEmpty(villageCode) || villageCode.contains(realPeopleHouse.getVillageCode())) {
					//小区名称/楼栋号/房间号
					Village village = CacheMap.VILLAGE_INFO_MAP.get(house.getVillageCode());
					house.setVillageCode(realPeopleHouse.getVillageCode());
					house.setVillageName(village!=null?village.getVillageName():"");
					house.setBuildingNo(realPeopleHouse.getBuildingNo());
					house.setUnitNo(realPeopleHouse.getUnitNo());
					house.setHouseNo(realPeopleHouse.getHouseNo());
					//所属辖区
					OrganizationVillage organizationVillage = CacheMap.VILLAGE_ORG_MAP.get(village.getParentId());
					house.setOrgName(organizationVillage!=null?organizationVillage.getOrgName():"");
					//房间类型
					if(house.getType()!=null){
			        	house.setTypeDesc(HouseTypeEnum.getNamebyValue(house.getType()));
			        }
				}
			}
		}
		return house;
	}
}
